package com.kool.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.leitu.bean.MenuBean;
import com.kool.user.bean.io.*;
import com.kool.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @description:用户接口
 * @author: tianhe
 * @create: 2019/02/01 15:57
 */
@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @param input
     * @return
     */
    @PostMapping("/user/register")
    public JSONObject register(@RequestBody String input) {
        JSONObject joIn = JSON.parseObject(input);
        JSONArray jaSecIn = joIn.getJSONArray("a");
        JSONObject joPropIn = jaSecIn.getJSONObject(0);
        RegisterRequest request = JSON.toJavaObject(joPropIn, RegisterRequest.class);

        userService.register(request);

        return new JSONObject();
    }

    /**
     * @return
     */
    @PostMapping("/user/preLogin")
    public JSONObject preLogin(@RequestBody String input) {
        JSONObject joIn = JSON.parseObject(input);
        JSONArray jaSecIn = joIn.getJSONArray("a");
        JSONObject joPropIn = jaSecIn.getJSONObject(0);
        PreLoginRequest request = JSON.toJavaObject(joPropIn, PreLoginRequest.class);

        PreLoginResponse response = userService.preLogin(request);

        JSONObject joOut = new JSONObject();
        JSONArray jaSecOut = new JSONArray();
        jaSecOut.add(JSON.toJSON(response));
        joOut.put("a", jaSecOut);

        return joOut;
    }

    /**
     * @return
     */
    @PostMapping("/user/login")
    public JSONObject login(@RequestBody String input) {
        JSONObject joIn = JSON.parseObject(input);
        JSONArray jaSecIn = joIn.getJSONArray("a");
        JSONObject joPropIn = jaSecIn.getJSONObject(0);
        LoginRequest request = JSON.toJavaObject(joPropIn, LoginRequest.class);

        LoginResponse response = userService.login(request);

        JSONObject joOut = new JSONObject();
        JSONArray jaSecOut = new JSONArray();
        jaSecOut.add(JSON.toJSON(response));
        joOut.put("a", jaSecOut);

        return joOut;
    }

    @PostMapping("/user/menus/get")
    public JSONObject getMenus(@RequestBody String input){
        JSONObject joIn = JSON.parseObject(input);
        JSONArray jaSecIn = joIn.getJSONArray("a");
        JSONObject joPropIn = jaSecIn.getJSONObject(0);
        MenusGetRequest request = JSON.toJavaObject(joPropIn, MenusGetRequest.class);
        List<MenuBean> menuBeans = userService.getMenusOfUser(request.getUserId());

        JSONObject joOut = new JSONObject();
        JSONArray jaSecOut = new JSONArray();
        jaSecOut.addAll(menuBeans);
        joOut.put("a", jaSecOut);
        return joOut;
    }

}
